
/*

This is a very rough draft of how the parser might work
I have a lot of questions, but I wanted to get started with something. 

Question 1: Our HTTPMsg doesnt have variables to store all the pieces of info that the example I used has
Question 2: is my example not a good one? 
Question 3: This code assumes that the struct is what contains all the info that is passed between the proxy and server, is this true? 
Question 4: What is the proper syntax for storing strings into a struct? Will string copy work? 
Question 5: I dont have it implimented but will strncasecmp work? We will need to count out the name of each bit of info, but I think it will still work. 
Question 6: Does the if/else if/ else combination work to identify the message? If the message is multiple lines it can just concat to the end of the current message
Question 7: would the parser be a seperate function? If so where would it be called and does it need arguments passed to it? 



*/



/* 

Used this for an example of what needs to be parsed: 
http://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039


POST /foo.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,**;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/test.php
Content-Type: application/x-www-form-urlencoded
Content-Length: 43
 
first_name=John&last_name=Doe&action=Submit

*/ 



#include <stdio.h>
#define BIGSIZE 10000



int main ()
{
	char line[BIGSIZE];
	int ContentLength; 
	char method[BIGSIZE], url[BIGSIZE], protocol[BIGSIZE];
	char Host[BIGSIZE]; 
	char UserAgent[BIGSIZE]; 
	char Accept[BIGSIZE]; 
	char Language[BIGSIZE]; 
	char Encoding[BIGSIZE]; 
	char Charset[BIGSIZE]; 
	char KeepAlive[BIGSIZE]; 
	char Referer[BIGSIZE]; 
	char Type[BIGSIZE]; 
	char Message[BIGSIZE]; 
	
	
	
	if ( !scanf("%s", method) ) printf("error message"); 
	if ( !scanf("%s", url) ) printf("error message"); 
	if ( !scanf("%s", protocol) ) printf("error message"); 
	
	strcpy(HTTPMsg.method, method); // is this the correct syntax? 
	
	
	// the headers after the first line will be read in as an entire line
	
	// does strlen work here? 
	while ( fgets( line, strlen[line], stdin ) != '\0' ) // while the line is not null
	{

		// if terminating character then break the while loop
		if ( strcmp( line, "\r\n" ) == 0 )
			break;
		// should this also check for: strcmp( line, "\n" ) == 0  

		/*
		// this is to determine the version, but it is dealt with above in the protocol scanf
		if ( strcmp( line[strlen[line]-9], "HTTP/1.1" ) == 0) // compares starting 8 chars from the end, because thats where HTTP/version would be written
			HTTPMsg.version = atof(line[strlen[line]-4]); // start writting from the 3rd char back
		// is this the correct syntax for atof? 
		
		
		*/ 
		
		// Should these else if store the values in the HTTPMsg struct?
		
		
		// each strcmp needs to only check the ammount of chars for each specific message
		// this might work with strncasecmp( line, "Content-Length:", 15 )
		// http://linux.die.net/man/3/strncasecmp 
		
		// strncasecmp( line, "Host: ", 5 )
		else if ( strcmp( line, "Host: ") == 0 )
			// store Host
			strcpy(Host, line[6]); // start at position 6
			// we can store directly into the struct from here
			// instead of into the Host char array I made.
			
		else if ( strcmp( line, "User-Agent: ") == 0 )
			// store UserAgent
			strcpy(UserAgent, line[11]); 
		else if ( strcmp( line, "Accept: ") == 0 )
			// store Accept
		else if ( strcmp( line, "Accept-Language: ") == 0 )
			// store Language
		else if ( strcmp( line, "Accept-Encoding: ") == 0 )
			// store Encoding
		else if ( strcmp( line, "Accept-Charset: ") == 0 )
			// store Charset
		else if ( strcmp( line, "Keep-Alive: ") == 0 )
			// store KeepAlive
		else if ( strcmp( line, "Connection: ") == 0 )
			// store Connection
		else if ( strcmp( line, "Referer: ") == 0 )
			// store Referer
		else if ( strcmp( line, "Conent-Type: ") == 0 )
			// store Type
		else if ( strcmp( line, "Content-Length:") == 0 )
			HTTPMsg.contentLength = atoi(line[15]); // the syntax is wrong here? 
			// start at position 15 because "Content-Length: " is 15 char long, so the number starts right after it      
		else // if none of the if/else if trigger then its the message
			// store message
		
		
	
	}

	// after the loop breaks all the values should be stored in the struct
	// this struct will then be passed into the socket
	
	
}